<template>
  <div class="findchange_wrap">
    <div style="text-align: center"><h1>重置密码</h1></div>
    <div class="form_warp">
      <el-form ref="formRef" :model="loginDate" class="demo-dynamic">
        <el-form-item prop="newpassword">
          <!-- v-model 数据的双向绑定 -->
          <el-input
            type="password"
            placeholder="请输入3位新密码"
            :prefix-icon="Lock"
            v-model="loginDate.newpassword"
            @blur="confirmnewpassword"
          />
        </el-form-item>
        <div v-if="vnewpassword">格式不正确</div>
        <el-form-item prop="password2">
          <!-- v-model 数据的双向绑定 -->
          <el-input
            type="password"
            placeholder="再次输入新密码"
            :prefix-icon="Lock"
            @blur="confirmnewpassword2"
            v-model="loginDate.newpassword2"
          />
        </el-form-item>
        <div v-if="vnewpassword2">两次密码不一致</div>
        <el-button
          class="findchange_btn"
          type="primary"
          @click="handlefindchange"
          round
          >确定</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { Lock } from '@element-plus/icons-vue'
import axios from 'axios'
import '../assets/css/all.css'
import { useRoute } from 'vue-router'
const useremail = useRoute()
console.log(useremail.query.email)
const loginDate = reactive({
  newpassword: '',
  newpassword2: ''
})
const vnewpassword = ref(false)
const vnewpassword2 = ref(false)
function confirmnewpassword() {
  if (!/^[a-zA-Z0-9]{6}$/.test(loginDate.newpassword)) vnewpassword.value = true
  else vnewpassword.value = false
}
function confirmnewpassword2() {
  if (!(loginDate.newpassword2 === loginDate.newpassword)) {
    vnewpassword2.value = true
  } else vnewpassword2.value = false
}

function handlefindchange() {
  axios({
    url: 'http://localhost:8080/user/forget/password',
    method: 'put',
    contentType: 'application/json',
    data: {
      phone: useremail.query.email,
      newPassword: loginDate.newpassword
    }
  }).then(function (response) {
    if (response.data.result === 'success') alert('修改成功')
    else if (response.data.result === 'fail') alert('修改失败')
    // alert(JSON.stringify(response.data))
    // console.log(response.data.result)
  })
}
</script>

<style lang="scss">
.findchange_wrap {
  height: 100%;
  margin: 0;
  background-image: url('../assets/1.jpg');
  background-size: 100% 100%;
  background-attachment: fixed;
  position: relative;
}
.form_warp {
  position: fixed;
  // background: floralwhite;
  width: 220px;
  height: 200px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.findchange_btn {
  margin: 20px auto;
  display: block;
  width: 100%;
  height: 28px;
}
</style>
